<!--
 * @Description: 产品原料
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-05-16 14:40:31
 * @FilePath: /yuxiuproweb/src/views/mes/pro/workorder/mods/add/raw.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    v-dialogDrag
    title="产品搜索"
    :visible.sync="shows"
    width="1060px"
    append-to-body
  >
    <div class="part">
      <div class="part-left">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="品名" prop="workorderCode">
            <el-input
              v-model="curtainQueryParams.name"
              placeholder="请输入品名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="">
            <el-radio-group
              v-model="curtainQueryParams.type"
              @change="handleQuery"
            >
              <el-radio label="0">面料</el-radio>
              <el-radio label="1">辅料</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <div style="margin-bottom: 40px">
          <el-table ref="multipleTable" :data="curtainList" :max-height="305">
            <el-table-column width="60" align="center">
              <template slot-scope="{ row }">
                <el-radio
                  v-model="row.checked"
                  class="radio-view"
                  :label="true"
                  @change="checkedChange(row, 'curtainList')"
                  ><i></i
                ></el-radio>
              </template>
            </el-table-column>
            <el-table-column label="品名" prop="name">
              <template slot-scope="{ row }">
                <el-link
                  type="primary"
                  @click="setImage(row), checkedChange(row, 'curtainList')"
                  >{{ row.name }}</el-link
                >
              </template>
            </el-table-column>
            <el-table-column
              label="相关信息"
              prop="marketingInfo"
            ></el-table-column>
            <el-table-column
              label="价格"
              prop="wholesalePrice1"
              width="80"
            ></el-table-column>
          </el-table>
          <pagination
            v-show="curtainTotal > 0"
            :total="curtainTotal"
            :page.sync="curtainQueryParams.pageNum"
            :limit.sync="curtainQueryParams.pageSize"
            @pagination="getCurtainList"
          />
        </div>
      </div>
      <div class="part-right ml10">
        <el-image :src="skuImg">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
    <!-- 配置高端定制 -->
    <detail-view
      v-if="detail.show"
      :show.sync="detail.show"
      v-bind="detail"
      @on-change="handleDetailChange"
    ></detail-view>
  </el-dialog>
</template>

<script>
import { listRawMaterial } from "@/api/goods/material";
import DetailView from "./otherDetail.vue";
export default {
  components: {
    DetailView,
  },
  props: {
    show: Boolean,
    type: {
      type: String,
      default: "add",
    },
    data: {
      type: Object,
    },
  },
  data() {
    return {
      // 查询参数
      curtainQueryParams: {
        pageNum: 1,
        pageSize: 100,
        name: null,
        type: "0",
      },
      detail: {
        show: false,
        data: null,
      },
      curtainTotal: 0,
      gdTotal: 0,
      curtainList: [],
      gdCurtainList: [],
      multipleSelection: [],
      queryParams: {},
      skuImg: "",
      checkedRow: null,
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getCurtainList();
  },
  methods: {
    getCurtainList() {
      listRawMaterial(this.curtainQueryParams).then(({ rows, total }) => {
        this.curtainList = rows?.map((e) => ({
          ...e,
          id: e.rawMaterialId,
          checked: this.checkedRow?.id === e.rawMaterialId,
          types: "curtain",
        }));
        this.curtainTotal = total;
      });
    },

    handleQuery() {
      this.getCurtainList();
    },
    resetQuery() {
      this.curtainQueryParams = {
        pageNum: 1,
        pageSize: 100,
        name: null,
        type: "0",
      };
      this.handleQuery();
    },
    /** 显示右侧图片 */
    setImage({ skuPicture }) {
      this.skuImg = skuPicture;
    },
    checkedChange(row, key) {
      this[key]?.forEach((e) => {
        if (e.id !== row.id) e.checked = false;
        else e.checked = true;
      });
      if (key !== "curtainList") {
        this.curtainList?.forEach((e) => (e.checked = false));
      } else {
        this.gdCurtainList?.forEach((e) => (e.checked = false));
      }

      this.checkedRow = {
        ...row,
      };
    },
    handleDetailChange(data) {
      const item = data?.find((e) => e.part === "-1" && e.part2 === "-1");
      console.log(item);
      this.$emit(
        "on-change",
        {
          ...this.detail.data,
          wholesalePrice: item?.wholesalePrice1 || 0,
        },
        data
      );
      this.shows = false;
    },
    /** 提交按钮 */
    async submitForm() {
      switch (this.checkedRow?.types) {
        case "gd":
          this.detail = {
            show: true,
            data: {
              ...this.checkedRow,
            },
          };
          break;
        case "curtain":
          this.$emit("on-change", this.checkedRow, []);
          this.shows = false;
          break;
        default:
          break;
      }
    },
  },
};
</script>

<style scoped lang="scss">
.part {
  width: 100%;
  display: flex;
  &-left {
    width: 600px;
  }
  &-right {
    flex: 1;
    border: 1px solid rgba(220, 220, 220, 0.5);
    border-radius: 4px;
    height: 422px;
    padding: 10px;
    img {
      width: 400px;
      height: 400px;
    }
    ::v-deep {
      .image-slot {
        width: 400px;
        height: 400px;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #909399;
      }
    }
  }
}
</style>
